package com.yunlan.materia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.yunlan.materia.entity.MateriaMedicaBook;
import com.yunlan.materia.enums.BaseStatusEnum;
import com.yunlan.materia.enums.BusinessEnum;
import com.yunlan.materia.enums.DeleteEnum;
import com.yunlan.materia.exceptions.GlobalException;
import com.yunlan.materia.mapper.MateriaMedicaBookMapper;
import com.yunlan.materia.request.DeleteDisableBookRequest;
import com.yunlan.materia.request.MateriaMedicaBookRequest;
import com.yunlan.materia.request.NormalAbleBookRequest;
import com.yunlan.materia.request.QueryBookRequest;
import com.yunlan.materia.response.MateriaMedicaBookResponse;
import com.yunlan.materia.service.MateriaMedicaBookService;
import com.yunlan.materia.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 云澜
 * @date 2025/9/17
 * @ApiNote
 */
@Slf4j
@Service
public class MateriaMedicaBookServiceImpl implements MateriaMedicaBookService {

    @Resource
    MateriaMedicaBookMapper materiaMedicaBookMapper;

    @Override
    public MateriaMedicaBook addBook(MateriaMedicaBookRequest request) {
        MateriaMedicaBook book = queryBookByName(request.getBookName());
        if (book != null) {
            throw new GlobalException(BusinessEnum.DATA_EXIST.getCode(),BusinessEnum.DATA_EXIST.getMsg());
        }

        MateriaMedicaBook entity = new MateriaMedicaBook();
        BeanCopier beanCopier = BeanCopier.create(MateriaMedicaBookRequest.class,MateriaMedicaBook.class,false);
        beanCopier.copy(request,entity,null);

        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        entity.setOperatingTime(now);
        entity.setDelete(DeleteEnum.NORMAL.getCode());
        entity.setBookStatus(BaseStatusEnum.NORMAL.getCode());
        entity.setUniqueCode(StringUtils.generatorUniqueCodeByType("book"));
        materiaMedicaBookMapper.insert(entity);
        return queryBookByName(request.getBookName());
    }

    @Override
    public int deleteDisableBook(DeleteDisableBookRequest request) {
        MateriaMedicaBook book = queryBookById(request.getBookId());
        if (book == null) {
            throw new GlobalException(BusinessEnum.DATA_NOT_EXIST.getCode(), BusinessEnum.DATA_NOT_EXIST.getMsg());
        }
        int result = 0;

        switch (request.getOperatorType()) {
            case 1:
                result = deleteBookById(request.getBookId(), request.getRemark(), request.getOperator());
                break;
            case 2:
                result = disableBookById(request.getBookId(), request.getRemark(), request.getOperator());
                break;
        }
        return result;
    }

    @Override
    public int normalAbleBook(NormalAbleBookRequest request) {
        return ableBookById(request.getBookId(), request.getRemark(), request.getOperator());
    }

    @Override
    public List<MateriaMedicaBookResponse> queryBookList(QueryBookRequest request) {
        materiaMedicaBookMapper.selectList(new LambdaQueryWrapper<MateriaMedicaBook>()
                .eq(StringUtils.isNotBlank(request.getBookId() + ""), MateriaMedicaBook::getId, request.getBookId())
                .eq(StringUtils.isNotBlank(request.getUniqueCode() ), MateriaMedicaBook::getUniqueCode, request.getUniqueCode())
                .like(StringUtils.isNotBlank(request.getBookName()), MateriaMedicaBook::getBookName, request.getBookName())
                .eq(StringUtils.isNotBlank(request.getBookAuthor()), MateriaMedicaBook::getBookAuthor, request.getBookAuthor())
        );
        return null;
    }

    @Override
    public MateriaMedicaBook queryBookByName(String bookName){
        return materiaMedicaBookMapper.selectOne(new QueryWrapper<MateriaMedicaBook>().lambda().eq(MateriaMedicaBook::getBookName,bookName));
    }

    @Override
    public  MateriaMedicaBook queryBookById(Long id){
        return materiaMedicaBookMapper.selectById(id);
    }

    @Override
    public int deleteBookById(Long id,String remark,String operator) {
        return materiaMedicaBookMapper.update(null, new LambdaUpdateWrapper<MateriaMedicaBook>()
                .eq(MateriaMedicaBook::getId, id)
                .set(MateriaMedicaBook::getRemark, remark)
                .set(MateriaMedicaBook::getDelete, DeleteEnum.DELETE.getCode())
                .set(MateriaMedicaBook::getOperator, operator));
    }

    @Override
    public int disableBookById(Long id, String remark, String operator) {
        return materiaMedicaBookMapper.update(null, new LambdaUpdateWrapper<MateriaMedicaBook>()
                .eq(MateriaMedicaBook::getId, id)
                .set(MateriaMedicaBook::getRemark, remark)
                .set(MateriaMedicaBook::getBookStatus, BaseStatusEnum.DISABLE.getCode())
                .set(MateriaMedicaBook::getOperator, operator));
    }

    @Override
    public int ableBookById(Long id, String remark, String operator) {
        return materiaMedicaBookMapper.update(null, new LambdaUpdateWrapper<MateriaMedicaBook>()
                .eq(MateriaMedicaBook::getId, id)
                .set(MateriaMedicaBook::getRemark, remark)
                .set(MateriaMedicaBook::getBookStatus, BaseStatusEnum.NORMAL.getCode())
                .set(MateriaMedicaBook::getOperator, operator));
    }
}
